import {login} from '@/api/login'
import {getToken, setToken, removeToken} from '@/utils/auth'

const user = {
    state: {
        token: getToken()
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        }
    },
    actions: {
        //登录
        Login({commit}, userInfo) {
            const username = userInfo.username.trim()
            const password = userInfo.password
            const code = userInfo.code
            const uuid = userInfo.uuid
            return new Promise((resolve, reject) => {
                login(username, password, code, uuid).then(res => {
                    //登录成功的请求
                    setToken(res.token);//将token设置到浏览器的cookie中
                    commit('SET_TOKEN', res.token)
                    resolve();//调用 resolve 函数时，它会将 Promise 的状态从“未完成”（pending）变为“成功”（fulfilled）
                }).catch(err => {
                    //登录出现的异常
                    reject(err)
                })
            })
        }
    }
}
export default user;
